//
//  THEarnMoneyVC.m
//  YJB
//
//  Created by 中电大维 on 2016/11/8.
//  Copyright © 2016年 YJB. All rights reserved.
//

#import "THEarnMoneyVC.h"
#import "Masonry.h"
#import "THTasklistVC.h"
#import "YJNetworkTool.h"
#import "YJUserDefaultTool.h"
#import "THEarningBrowserVC.h"
#import "THFlowEarnPublishTaskVC.h"
#import "THFlowEarnManaulPublishVC.h"

@interface THEarnMoneyVC ()
@property(nonatomic,strong)UIView  *headView;
@property(nonatomic,weak)UILabel   *gold;
@property(nonatomic,weak)UISwitch *switchBtn;
@property(nonatomic,weak)UILabel *equal;
@property(nonatomic,weak)UILabel *maxCount;
@property(nonatomic,strong)NSArray  *titles;
@end

@implementation THEarnMoneyVC
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"流量赚钱";
    self.tableView.tableFooterView = [UIView new];
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tableView.backgroundColor = kUIColorFromRGB(0xeeeeee);
    [self.tableView setSeparatorColor:[UIColor colorWithHexString:@"e5e5e5"]];
    //self.tableView.tableHeaderView = self.headView;
    _titles = @[@"开启流量",@"发布任务"];
    //self.equal.text = @"约0.012元";
    [self loadData];
}

- (void)loadData
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"action"] = @"get_traff_state";
    params[@"makegold"] = @{@"username":[YJUserDefaultTool readUserName],
                            @"user_info":[YJUserDefaultTool readUserName],
                            @"phone_info":@"iOS",
                            @"md5_key":[YJUserDefaultTool readKeyID]};
    [YJNetworkTool postWithUrl:THURL(@"traffic.php") parameters:params success:^(id responseObject) {
        NSString *code = responseObject[@"return"][@"code"];
        if (code.integerValue == 200)
        {
            NSDictionary *dataDict = responseObject[@"return"][@"data"];
            self.gold.text = [NSString stringWithFormat:@"%@金币",dataDict[@"traff_gold_not"]];
            self.equal.text = [NSString stringWithFormat:@"约%@元",dataDict[@"traff_rmb_not"]];
            self.switchBtn.on = [dataDict[@"traff_status"] boolValue];
            
//            NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc]initWithString:@"注:" attributes:@{NSForegroundColorAttributeName:kUIColorFromRGB(0xfa5f3c)}];
//            [attrString appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"  达到%@金币时自动转账",dataDict[@"auto_account_val"]] attributes:@{NSForegroundColorAttributeName:kUIColorFromRGB(0xcccccc)}]];
//            self.maxCount.attributedText = attrString;
            if (self.switchBtn.on)
            {
                _titles = @[@"开启流量",@"去赚钱",@"发布任务"];
                [self.tableView reloadData];
            }
            
        }else
        {
            NSString *message = responseObject[@"return"][@"message"];
            [MBProgressHUD showError:message toView:self.view];
        }
    } failure:^(NSError *error) {
         [MBProgressHUD showError:@"网络不佳，稍后重试" toView:self.view];
    }];
}

- (UIView *)headView
{
    if (_headView == nil)
    {
        _headView = [[UIView alloc] init];
        _headView.backgroundColor = WHITECOLOR;
        _headView.height = 135;
        UIImageView *imgview = [[UIImageView alloc] initWithImage:Image(@"钱包")];
        [self.headView addSubview:imgview];
        [imgview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(12);
            make.height.width.mas_equalTo(36);
            make.top.mas_equalTo(12);
        }];
        UILabel *label = [[UILabel alloc] init];
        [_headView addSubview:label];
        label.font = FONT(13);
        label.text = @"当前已赚\n金币";
        label.numberOfLines = 0;
        label.textAlignment = NSTextAlignmentLeft;
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(imgview.mas_right).offset(8);
            make.top.mas_equalTo(imgview.mas_top);
            make.bottom.mas_equalTo(imgview.mas_bottom);
            make.width.mas_equalTo(60);
        }];
        
        UIView *line = [[UIView alloc]init];
        line.backgroundColor = kUIColorFromRGB(0xfa5f3c);
        [_headView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(0);
            make.height.mas_equalTo(0.5);
        }];
    }
    return _headView;
}

- (UILabel *)gold
{
    if (_gold == nil)
    {
        UILabel *lab = [[UILabel alloc] init];
        lab.textAlignment = NSTextAlignmentCenter;
        [self.headView addSubview:lab];
        lab.font = FONT(25);
        lab.textColor = kUIColorFromRGB(0xfa5f3c);
        lab.text = @"12金币";
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(27);
            make.width.mas_equalTo(150);
            make.top.mas_equalTo(44);
            make.centerX.mas_equalTo(self.headView);
        }];
        _gold = lab;
    }
    return _gold;
}

- (UILabel *)equal
{
    if (_equal == nil)
    {
        UILabel *lab = [[UILabel alloc] init];
        [self.headView addSubview:lab];
        lab.font = FONT(16);
        lab.textColor = kUIColorFromRGB(0xfa5f3c);
        lab.textAlignment = NSTextAlignmentCenter;
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.gold.mas_centerX);
            make.height.mas_equalTo(18);
            make.width.mas_equalTo(280);
            make.top.mas_equalTo(self.gold.mas_bottom).offset(6);
        }];
        _equal = lab;
        
        UILabel *tip = [[UILabel alloc]init];
        tip.textAlignment = NSTextAlignmentCenter;
        tip.font = FONT(15);
        NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc]initWithString:@"注:" attributes:@{NSForegroundColorAttributeName:kUIColorFromRGB(0xfa5f3c)}];
        [attrString appendAttributedString:[[NSAttributedString alloc] initWithString:@"  达到2000金币时自动转账" attributes:@{NSForegroundColorAttributeName:kUIColorFromRGB(0xcccccc)}]];
        tip.attributedText = attrString;
        [self.headView addSubview:tip];
        [tip mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.mas_equalTo(lab.mas_bottom).offset(12);
            make.height.mas_equalTo(16);
        }];
        self.maxCount = tip;
    }
    return _equal;
}

#pragma mark ----------------------- 表格代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _titles.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *ID = @"eranMoneyCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        UISwitch *switchBtn = [[UISwitch alloc]init];
        [cell addSubview:switchBtn];
        [switchBtn addTarget:self action:@selector(switchBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        switchBtn.onTintColor = kUIColorFromRGB(0xfa5f3c);
        switchBtn.tag = 10;
        [switchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(cell.mas_centerY);
            make.right.mas_equalTo(cell).offset(-15);
        }];
    }
    UISwitch *switchBtn = (UISwitch *)[cell viewWithTag:10];
    if (indexPath.section == 0)
    {
        self.switchBtn = switchBtn;
        switchBtn.hidden = NO;
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = [UIColor colorWithHexString:@"e5e5e5"];
        [cell addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(cell.mas_bottom).offset(-0.5);
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(0);
            make.height.mas_equalTo(0.5);
        }];
    }else{
      switchBtn.hidden = YES;
      cell.selectionStyle = UITableViewCellSelectionStyleDefault;
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    cell.textLabel.text = _titles[indexPath.section];
    cell.textLabel.font = [UIFont systemFontOfSize:18];
    cell.textLabel.textColor = kUIColorFromRGB(0x333333);
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {//开启量自动赚钱
        if (0 == indexPath.row) {
            return;
        }
    }else if (indexPath.section == _titles.count -1){//发布任务
        //[self.navigationController pushViewController:[THFlowEarnPublishTaskVC new] animated:YES];
        [self.navigationController pushViewController:[THFlowEarnManaulPublishVC new] animated:YES];
        return;
    }
    [self.navigationController pushViewController:[THEarningBrowserVC new] animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0 || section == 2) {
        return 15;
    }
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = kUIColorFromRGB(0xeeeeee);
    return view;
}

- (void)switchBtnClick:(UISwitch *)switchBtn
{
    NSString *status = switchBtn.on?@"1":@"0";
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"action"] = @"set_traff_turnoff";
    params[@"makegold"] = @{@"username":[YJUserDefaultTool readUserName],@"status":status,
                            @"user_info":[YJUserDefaultTool readUserName],
                            @"phone_info":@"iOS",
                            @"md5_key":[YJUserDefaultTool readKeyID]};
    [MBProgressHUD showMessage:@"" toView:self.view];
    [YJNetworkTool postWithUrl:THURL(@"traffic.php") parameters:params success:^(id responseObject) {
        NSString *code = responseObject[@"return"][@"code"];
        [MBProgressHUD hideHUDForView:self.view];
        if (code.integerValue == 200)
        {
            if (self.switchBtn.on)
                _titles =  @[@"开启流量",@"去赚钱",@"发布任务"];
            else
              _titles =  @[@"开启流量",@"发布任务"];
             [self.tableView reloadData];
        }else
        {
            NSString *message = responseObject[@"return"][@"message"];
            [MBProgressHUD showError:message toView:self.view];
            switchBtn.on = !switchBtn.on;
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"网络不佳，稍后重试" toView:self.view];
        switchBtn.on = !switchBtn.on;
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadData];
}

@end
